package com.tange.ai.iot.client.gnssa.lan

import android.graphics.Color
import android.os.Build
import android.os.Bundle
import android.view.View
import android.view.WindowManager
import androidx.appcompat.app.AppCompatActivity
import com.tange.ai.iot.client.gnssa.databinding.ActivityLanConnectConfigureBinding
import com.tange.ai.iot.client.gnssa.play.DeviceLiveActivity
import com.tange.ai.iot.client.gnssa.utils.Utils
import com.tange.base.toolkit.PreferenceUtil
import com.tange.base.toolkit.onClicked
import com.tange.base.ui.design.BasicDialogLoading

class LanConfigureActivity: AppCompatActivity() {

    companion object {
        const val PRE_DEVICE_IP = "PRE_DEVICE_IP"
        const val PRE_DEVICE_ID = "PRE_DEVICE_ID"
        const val PRE_CONNECT_TOKEN = "PRE_CONNECT_TOKEN"
    }

    private lateinit var binding: ActivityLanConnectConfigureBinding

    private var loading: BasicDialogLoading? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        binding = ActivityLanConnectConfigureBinding.inflate(layoutInflater).apply {

            setContentView(root)
            supportActionBar?.hide()

            connect.onClicked {
                DeviceLiveActivity.launchLanMode(this@LanConfigureActivity, "xxx")
            }
        }

        window.apply {
            var uiFlags = View.SYSTEM_UI_FLAG_LAYOUT_STABLE
            uiFlags = uiFlags or View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
            clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS)
            addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS)
            statusBarColor = Color.TRANSPARENT
            decorView.systemUiVisibility = uiFlags
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                decorView.systemUiVisibility = View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN or View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR
            }
        }

        loading = BasicDialogLoading(this).apply {
            setMessage("尝试连接中...")
        }

        Utils.setStatusBar(this, Color.TRANSPARENT)

        val deviceIp = PreferenceUtil.getString(this, PRE_DEVICE_IP, "")
        val deviceId = PreferenceUtil.getString(this, PRE_DEVICE_ID, "")
        val connectToken = PreferenceUtil.getString(this, PRE_CONNECT_TOKEN, "")

        binding.deviceIp.setText(deviceIp)
        binding.deviceId.setText(deviceId)
        binding.connectToken.setText(connectToken)
    }

    override fun onPause() {
        super.onPause()
        loading?.dismiss()
    }
}